
Sprite = {
	
	-- Cria a tileset baseado no número de tiles por linha(sx)
	-- e por coluna(sy)
	newSprite = function(entity, sx, sy, noNation)
		local imageName = (noNation and entity.attributes.imageName) or (entity.nation .. entity.attributes.imageName);
		local s = {};
		setmetatable(s, {__index = Sprite});
	
		s.active = true;
		s.hpBar = true;
		s.dBoundingBox = false;
		s.entity = entity;
		s.fpsCount = 0;
		s.frame = 1; -- Controla o índice dos frames
		s.image = love.graphics.newImage('resource/' .. imageName);
		s.quads = {};
		s.angle = 0;
		
		s.qxSize = s.image:getWidth() / sx;  -- Largura de cada sprite
		s.qySize = s.image:getHeight() / sy; -- Altura de cada sprite
		
		--[[
			Padrão para as sprites, ex: 3x3
			1,2,3
			4,5,6
			7,8,9
		]]--
		
		for y = 0 , sy-1  do
			for x = 0, sx-1 do
				local quad = love.graphics.newQuad(x * s.qxSize, y * s.qySize, 
									   s.qxSize, s.qySize, 
									   s.image:getWidth(), s.image:getHeight());					
				table.insert(s.quads, quad);
			end
		end
	
		
		return s;
	end,
	
	setActive = function(self, flag)
		self.active = flag;
	end,
	
	showHPBar = function(self, flag) 
		self.hpBar = flag;
	end,
	
	setState = function(self, state)
		-- Se não estiver entrando em um novo estado, não reseta o frame
		if state ~= self.state then 
			self.frame = 1; 
		end
		self.state = state;
		
		-- Armazena a lista de frames para a animação
		self.stateSprites = self.entity.attributes.sprites[state];
		self.maxFrame = #self.stateSprites;
	end,
	
	update = function(self, dt)
		if self.active then
			self.fpsCount = self.fpsCount + dt;
			if self.fpsCount >= (1.0 / 15.0) then
				self.frame = self.frame + 1; 
			
				if self.frame > self.maxFrame then 
					self.frame = 1;
				end
			
				self.fpsCount = 0;
			end	
		end
		
		-- Caso esta animação esteja configurada para não se repetir
		if self.stateSprites[self.frame] == 0 then
			self:setActive(false);
		end
	end,
	
	flipStateSprites = function(self, x, y)
		local fliped = {};
		for _,iQuad in pairs(self.stateSprites) do
			if iQuad ~= 0 and not fliped[iQuad] then
				self.quads[iQuad]:flip(x, y);
				fliped[iQuad] = true;
			end
		end
	end,
	
	flipAllStateSprites = function(self, x, y)
		local fliped = {};
		for _,state in pairs(States) do
			for _,iQuad in ipairs(self.entity.attributes.sprites[state]) do
				if iQuad ~= 0 and not fliped[iQuad] then
					self.quads[iQuad]:flip(x, y);
					fliped[iQuad] = true;
				end
			end
		end
	end,
	
	drawBoundingBox = function(self, flag)
		self.dBoundingBox = flag;
	end,
	
	-- Use este método depois de chamar update, para ter o frame atual
	getCurrentQuad = function(self)
		return self.quads[self.stateSprites[self.frame]];
	end,
	
	draw = function(self, x, y)
		love.graphics.push();
		love.graphics.translate(self.entity.center.x,self.entity.center.y);
		love.graphics.rotate(self.angle * 180.0 / 360.0);
		love.graphics.translate(-self.entity.center.x,-self.entity.center.y);
		if self.active then
			local quad = self:getCurrentQuad();
			love.graphics.setColor(255,255,255);
			love.graphics.drawq(self.image, quad, x, y);
			
			-- Desenha a barra de vida
			if self.hpBar then
				local oY = y - 2;
				local hp = 0;
				
				if self.entity.attributes.hp > 0 then 
					hp = self.entity.attributes.hp;
				else 
					hp = 0;
				end
				
				local currHP = (self.qxSize * hp) / self.entity.origin.hp; 
				
				love.graphics.setColor(0,0,0);
				love.graphics.rectangle("fill", x - 1, oY - 2, self.qxSize + 2, 4);
				
				love.graphics.setColor(255,0,0);
				love.graphics.line(x, oY, x + self.qxSize, oY);
				
				love.graphics.setColor(0,255,0);
				love.graphics.line(x, oY, 
				                   x + currHP, oY);
			end
			
			-- Desenha a caixa de colisão
			if self.dBoundingBox then
				local boxX = self.entity.attributes.box.x;
				local boxY = self.entity.attributes.box.y;
				local boxW = self.entity.attributes.box.w;
				local boxH = self.entity.attributes.box.h;
				
				love.graphics.setColor(0,0,0);
				love.graphics.rectangle("line", boxX, boxY, boxW, boxH);
				love.graphics.setColor(255,0,0);
				love.graphics.circle("fill", self.entity.center.x, self.entity.center.y, 5);
				love.graphics.line(self.entity.center.x - self.entity.attributes.area, self.entity.center.y,
								   self.entity.center.x + self.entity.attributes.area, self.entity.center.y);
			end
			
		end
		love.graphics.pop(); 
	end
};
